# @Time:   2021/3/1 9:31
# @Author: Zong Wei

from datetime import date
from time import sleep

from faker import Faker
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.wait import WebDriverWait

from ui_automation_testing.test.pageObject.loginPage import LoginPage
from ui_automation_testing.utils.logger import Logger


class StationFeePage(LoginPage):
    logger = Logger(__name__).get_log()
    menu = (By.XPATH, "//ul[@class='site-menu']//span[.='业务通用']")
    sub_menu = (By.XPATH, "//ul[@class='site-menu']//span[.='站点费用']")
    search_button = (By.ID, "lr-search")
    add_button = (By.ID, "lr-add")
    edit_button = (By.ID, "lr-edit")
    del_button = (By.ID, "lr-delete")
    save_button = (By.ID, "lr-save")
    clear_button = (By.ID, "lr-clear")
    search_self_button = (By.ID, "lr-searchSelf")

    station_name = (By.ID, "StationName")
    biz_class = (By.ID, "ProductModeName")
    append_fee = (By.XPATH, "//a[@onclick='btn_add()']")
    fee_name = (By.ID, "FeeTypeNameC")




    end_date = (By.ID, "EndDate")
    supplier_name = (By.ID, "_easyui_textbox_input2")
    supplier_value = (By.ID, "datagrid-row-r2-2-0")
    entity_name = (By.ID, "_easyui_textbox_input1")
    entity_value = (By.ID, "datagrid-row-r1-2-0")
    sign_property = (By.ID, "SignProperty")
    sign_property_value = (By.XPATH, "//div[@id='SignProperty-option']//li[2]")
    version = (By.ID, "feeVersion")
    version_value = (By.XPATH, "//div[@id='feeVersion-option']//li[2]")
    fee_property = (By.ID, "feeProperty")
    fee_property_value = (By.XPATH, "//div[@id='feeProperty-option']//li[2]")
    applier = (By.ID, "_easyui_textbox_input4")
    applier_value = (By.ID, "datagrid-row-r4-2-0")
    supplier_class = (By.ID, "SupplierClass")
    supplier_class_value = (By.XPATH, "//div[@id='SupplierClass-option']//li[2]")
    status = (By.ID, "feeStatus")
    status_value = (By.XPATH, "//div[@id='feeStatus-option']//li[3]")

    remark = (By.ID, "Remark")
    confirm_button = (By.XPATH, "//a[.='确认']")
    first_row = (By.XPATH, "//span[.='站点代码']//following::tr[1]")
    first_fee = (By.ID, "datagrid-row-r1-2-0")
    ok_button = (By.CLASS_NAME, "layui-layer-btn0")

    def __init__(self):
        self.driver = LoginPage().login(name='05946', pwd='mw123')
        self.wait = WebDriverWait(self.driver, 10)

    def station_fee_home(self):
        self.logger.info("登录系统成功")
        self.wait.until(ec.element_to_be_clickable(self.menu)).click()
        sleep(1)
        # self.driver.execute_script('$("#admui-navTabs").scrollTop(200)')
        self.wait.until(ec.element_to_be_clickable(self.sub_menu)).click()
        sleep(1)
        return self.driver

    def search_fee(self, station='金桥', biz_class='海运出口'):
        self.driver.switch_to.frame(1)
        self.wait.until(ec.presence_of_element_located(self.station_name)).send_keys(station)
        sleep(1)
        self.wait.until(ec.presence_of_element_located(self.biz_class)).send_keys(biz_class)
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.search_button)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.clear_button)).click()
        self.logger.info("查询完成")

    def config_fee(self):
        self.search_fee()
        self.wait.until(ec.element_to_be_clickable(self.first_row)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.edit_button)).click()
        sleep(2)
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame(2)
        self.wait.until(ec.element_to_be_clickable(self.append_fee)).click()
        sleep(1)
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame(3)
        self.wait.until(ec.presence_of_element_located(self.fee_name)).send_keys('THC')
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.search_button)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.first_fee)).click()
        self.driver.switch_to.default_content()
        # self.wait.until(ec.element_to_be_clickable(self.ok_button)).click()
        self.logger.info("配置费用项完成")


if __name__ == '__main__':
    pg = StationFeePage()
    pg.station_fee_home()
    pg.config_fee()

